package great.easychat.help.activity

import android.Manifest
import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.LayoutInflater
import androidx.core.content.PermissionChecker
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import great.easychat.help.BaseActivity
import great.easychat.help.R
import great.easychat.help.util.PermissionGoUtil
import great.easychat.help.viewModel.SplashViewModel
import kotlinx.android.synthetic.main.activity_permisson_setting.*
import kotlinx.android.synthetic.main.item_permisson_setting.view.*

class PermissonSettingActivity : BaseActivity<SplashViewModel>() {

    override fun setViewData(savedInstanceState: Bundle?): Unit {
        recyPer.layoutManager = LinearLayoutManager(this)
        recyPer.adapter = adapter
        adapter.setNewData(mPrivacys)
        check()
        setTitle("隐私权限设置")
    }

    override fun requestLayoutId(): Int {
        return R.layout.activity_permisson_setting

    }

    private val mPrivacys = listOf<Privacy>(
            Privacy(Manifest.permission.READ_PHONE_STATE, "允许查看电话信息", false, "用于获取设备信息，崩溃日志搜集等功能\n" +
                    "[关闭电话权限，可能导致播放异常或退出]", "/privacyscPhone"),
            Privacy(Manifest.permission.READ_EXTERNAL_STORAGE, "允许使用文件存储和访问功能", false, "用于浏览视频，缓存图片或视频功能\n" +
                    "[关闭存储权限，可能导致播放异常或退出]", "/privacyscFile"))



    private val adapter by lazy {
        PrivacyAdapter().apply { addHeaderView(LayoutInflater.from(this@PermissonSettingActivity).inflate(R.layout.header_pravice, null, false)) }
    }


    @SuppressLint("WrongConstant")
    private fun check() {
        adapter.data.forEach {
            it.isGet = (PermissionChecker.checkSelfPermission(this, it.permissionKey) == PackageManager.PERMISSION_GRANTED)
        }
        adapter.notifyDataSetChanged()
    }

    override fun onResume() {
        super.onResume()
        check()
    }


    class Privacy(val permissionKey: String, val title: String, var isGet: Boolean, var desc: String, var webUrl: String) {

    }

    class PrivacyAdapter : BaseQuickAdapter<Privacy, BaseViewHolder>(R.layout.item_permisson_setting, ArrayList<Privacy>()) {
        override fun convert(helper: BaseViewHolder, item: Privacy) {
            helper.itemView.tvTittle.text = item.title
            helper.itemView.tvDesc.text = item.desc
            helper.itemView.tvStatus.text = if (item.isGet) "已开启" else "去设置"
            helper.itemView.setOnClickListener {
                PermissionGoUtil.gotoPermission(mContext)
            }
            helper.itemView.tvShowDetails.setOnClickListener {


            }

        }
    }

    override fun observeLiveData() {

    }
}
